package com.changgou.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.search.dao.ESManagerMapper;
import com.changgou.search.pojo.SkuInfo;
import com.changgou.search.service.ESManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ESManagerServiceImpl implements ESManagerService {

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @Autowired
    private SkuFeign skuFeign;

    @Autowired
    private ESManagerMapper esManagerMapper;

    /**
     * 创建索引结构
     */
    @Override
    public void createMappingAndIndex() {
        //创建索引
        elasticsearchTemplate.createIndex(SkuInfo.class);
        //创建映射
        elasticsearchTemplate.putMapping(SkuInfo.class);
    }

    /**
     * 导入所有数据到ES索引库
     */
    @Override
    public void importAll() {
        //查询sku集合
        List<Sku> skuList = skuFeign.findSkuListBySpuId("all");
        //进行 list 的判断
        if (skuList == null || skuList.size()<=0){
            throw new RuntimeException("当前没有数据被查询到，无法导入索引库");
        }
        //将 skuList数据转换成 JSON 格式
        String jsonString = JSON.toJSONString(skuList);
        //将 json  转换成  skuInfo
        List<SkuInfo>  skuInfoList=JSON.parseArray(jsonString,SkuInfo.class);

        for (SkuInfo skuInfo : skuInfoList) {
            //将规格信息转换成map
            String spec = skuInfo.getSpec();
            Map specMap = JSON.parseObject(spec, Map.class);
            skuInfo.setSpecMap(specMap);
        }

        //导入索引库
        esManagerMapper.saveAll(skuInfoList);
    }

    /**
     * 根据 spuId 导入数据到ES索引库
     * @param spuId 商品id
     */
    @Override
    public void importDataBySpuId(String spuId) {

        //查询sku集合
        List<Sku> skuList = skuFeign.findSkuListBySpuId(spuId);
        if (skuList == null || skuList.size()<=0){
            throw new RuntimeException("当前没有数据被查询到，无法导入索引库");
        }
        //将skuList转换成Json
        String jsonString = JSON.toJSONString(skuList);
        //将 JSON 转换成 skuInfo
        List<SkuInfo> skuInfoList = JSON.parseArray(jsonString, SkuInfo.class);
        for (SkuInfo skuInfo : skuInfoList) {
            //将 spec转换成map
            Map specMap = JSON.parseObject(skuInfo.getSpec(), Map.class);
            skuInfo.setSpecMap(specMap);
        }
        //导入索引库
        esManagerMapper.saveAll(skuInfoList);
    }

    /**
     * 根据  spuId 将 下架的商品 ，从 索引库中删除
     * @param spuId
     */
    @Override
    public void deleteDataBySpuId(String spuId) {
        List<Sku> skuList = skuFeign.findSkuListBySpuId(spuId);
        if (skuList == null || skuList.size()<= 0 ){
            throw new RuntimeException("当前没有数据被查询到，无法从索引库中删除");
        }
        for (Sku sku : skuList) {
            esManagerMapper.deleteById(Long.parseLong(sku.getId()));
        }
    }
}
